package com.example.springcloud.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.example.springcloud.domain.Products;
import com.example.springcloud.service.IProductsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品信息Controller
 * 
 * @author hx
 * @date 2025-02-14
 */
@RestController
@RequestMapping("/group_buy/products")
@Api(tags = "商品信息管理")
public class ProductsController extends BaseController
{
    @Autowired
    private IProductsService productsService;

    /**
     * 查询商品信息列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询商品信息列表  allmList ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "分类ID", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "regionId", value = "区域ID", required = false, dataType = "Long")
    })
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功", response = Products.class, responseContainer = "List") })
    public TableDataInfo list(Long categoryId, Long regionId)
    {
        startPage();
        Products products = new Products();
        products.setCategoryId(categoryId);
        products.setRegionId(regionId);
        List<Products> list = productsService.selectProductsList(products);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('group_buy:products:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Products products)
    {
        List<Products> list = productsService.selectProductsList(products);
        ExcelUtil<Products> util = new ExcelUtil<Products>(Products.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @GetMapping(value = "/{productId}")
    @ApiOperation(value = "获取商品信息详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品ID", required = true, dataType = "Long")
    })
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功", response = Products.class) })
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(productsService.selectProductsByProductId(productId));
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('group_buy:products:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Products products)
    {
        return toAjax(productsService.insertProducts(products));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('group_buy:products:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Products products)
    {
        return toAjax(productsService.updateProducts(products));
    }

    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('group_buy:products:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(productsService.deleteProductsByProductIds(productIds));
    }
}
